<template>
	<my-form ref="formObj" :title="completeTitle" :readonly="readonly" @form-alert-dismissed-success="formViewBack"
	 @form-reset="formReset" @form-submit="formSubmit" @form-view-back="formViewBack">
		<b-form-group label="标签名称" label-for="name-elem">
			<b-form-input id="name-elem" required :readonly="readonly" v-model="form.name" placeholder="标签名称"></b-form-input>
		</b-form-group>
		<b-form-group label="标签别名" label-for="alias-name-elem">
			<b-form-input id="alias-name-elem" :readonly="readonly" required v-model="form.aliasName" placeholder="标签别名"></b-form-input>
		</b-form-group>
		<b-form-group label="标签备注" label-for="remark-elem">
			<b-form-textarea id="remark-elem" :readonly="readonly" v-model="form.remark" placeholder="标签备注" rows="3"></b-form-textarea>
		</b-form-group>
	</my-form>
</template>

<script>
	import axios from 'axios'
	import gl from '../commons/global_variable.js'
	import MyForm from '../commons/Form.vue'

	export default {
		name: "TagDetails",
		components: {
			MyForm
		},
		data: function() {
			return {
				title: "标签",
				form: {
					id: 0,
					name: '',
					aliasName: '',
					remark: ''
				},
				readonly: false
			}
		},
		created: function() {
			this.$store.commit("menuFoucsOpr", "tags")
			if (this.$route.params.tagId) {
				var that = this
				axios.get(gl.cms_args.server_url + 'tag/detail?id=' + this.$route.params.tagId).then(function(response) {
					var res = response.data
					if (res.code === gl.cms_args.success) {
						that.form = res.data
					} else {
						that.$refs.formObj.onQueryError()
					}
				}).catch(function(error) {
					that.$refs.formObj.onQueryError()
				})
			}
			if (this.$route.params.readonly === true) {
				this.readonly = true
			}
		},
		methods: {
			formViewBack: function() {
				this.$router.push({
					path: "tags"
				})
			},
			formSubmit: function() {
				var that = this
				axios.post(gl.cms_args.server_url + 'tag/sou', this.form)
					.then(function(response) {
						if (response.data.code === gl.cms_args.success) {
							that.$refs.formObj.onSaveSuccess()
						} else {
							that.$refs.formObj.onSaveError()
						}
					}).catch(function(error) {
						that.$refs.formObj.onSaveError()
					})
			},
			formReset: function() {
				this.form.name = ''
				this.form.aliasName = ''
				this.form.remark = ''
			}
		},
		computed: {
			completeTitle: function() {
				return this.$route.params.title + this.title
			}
		}
	}
</script>

<style>
</style>
